package io.geekidea.boot.test.controller;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.test.dto.CarsDto;
import io.geekidea.boot.test.query.CarsQuery;
import io.geekidea.boot.test.service.CarsService;
import io.geekidea.boot.test.vo.CarsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 车辆 控制器
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@RestController
@Tag(name = "车辆")
@RequestMapping("/admin/cars")
public class CarsController {

    @Autowired
    private CarsService carsService;

    /**
     * 添加车辆
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加车辆")
    @PostMapping("/addCars")
    @Permission("cars:add")
    public ApiResult addCars(@Valid @RequestBody CarsDto dto) {
        log.info("添加车辆：{}", dto);
        boolean flag = carsService.addCars(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改车辆
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改车辆")
    @PostMapping("/updateCars")
    @Permission("cars:update")
    public ApiResult updateCars(@Valid @RequestBody CarsDto dto) {
        log.info("修改车辆：{}", dto);
        boolean flag = carsService.updateCars(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除车辆
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除车辆")
    @PostMapping("/deleteCars/{id}")
    @Permission("cars:delete")
    public ApiResult deleteCars(@PathVariable Long id) {
        log.info("删除车辆：{}", id);
        boolean flag = carsService.deleteCars(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取车辆详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取车辆详情")
    @PostMapping("/getCars/{id}")
    @Permission("cars:info")
    public ApiResult<CarsVo> getCars(@PathVariable Long id) {
        log.info("获取车辆详情：{}", id);
        CarsVo carsVo = carsService.getCarsById(id);
        return ApiResult.success(carsVo);
    }

    /**
     * 获取车辆分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取车辆分页列表")
    @PostMapping("/getCarsPage")
    @Permission("cars:page")
    public ApiResult<CarsVo> getCarsPage(@Valid @RequestBody CarsQuery query) {
        log.info("获取车辆分页列表：{}", query);
        Paging<CarsVo> paging = carsService.getCarsPage(query);
        return ApiResult.success(paging);
    }

}
